package com.example.kotlinfindcar

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatDelegate
import com.alibaba.android.arouter.BuildConfig
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.Utils
import com.example.kt_all.di.allModule
import com.example.kt_common.util.AppCommonUitl
import com.example.kt_common.util.AppLogUtil
import com.example.kt_common.util.Constants
import com.example.kt_common.util.KVUtil
import com.example.kt_common.util.ToastUtil
import com.example.kt_fast.di.fastModule
import com.example.kt_user.di.userModule
import com.tencent.mmkv.MMKV
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
/**
 * Created by 小陈 on 2025/3/23.
 * Time: 16:35.
 */
class MyApplication :Application() {
    private val TAG = MyApplication::class.java.name
    private val modules = mutableListOf(fastModule,userModule, allModule)

    companion object{
        var instance: MyApplication? = null
    }
    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        //HookClassLoader.hook(this)
    }
    override fun onCreate() {
        super.onCreate()
        instance = this
        ToastUtil.init(instance!!)
        val oldTime = System.currentTimeMillis()
        initKoin()
        initARouter()
        initMMKV()
        AppLogUtil.init(instance!!)
        AppCommonUitl.init(instance!!)
        initUserDarkMode()
        Utils.init(this)  // 初始化 UtilCode
        registerActivityLifecycle()
        Log.d(TAG, "Third party cost time : ${System.currentTimeMillis() - oldTime}")
        //test async third party
        //initAsyncThirdParty()  //android 启动优化
    }
    private fun initKoin() {
        startKoin {
            androidLogger()
            androidContext(this@MyApplication)
            modules(modules)
        }
    }
    private fun initARouter() {
        if (BuildConfig.DEBUG) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(this)
    }
    private fun initMMKV() {
        MMKV.initialize(this)
    }
    private fun initUserDarkMode() {
        val userDarkMode = KVUtil.getInt(Constants.USER_DARK_MODE, 999)
        Log.d(TAG, "userDarkMode: $userDarkMode")
        when (userDarkMode) {
            999 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            1000 -> AppCommonUitl.setFollowSystemTheme()
            else -> AppCompatDelegate.setDefaultNightMode(userDarkMode)
        }
    }
    private fun registerActivityLifecycle() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                Log.d(TAG, "onActivityCreated: " + activity.localClassName)
            }

            override fun onActivityStarted(activity: Activity) {
                Log.d(TAG, "onActivityStarted: " + activity.localClassName)
            }

            override fun onActivityResumed(activity: Activity) {
                Log.d(TAG, "onActivityResumed: " + activity.localClassName)
            }

            override fun onActivityPaused(activity: Activity) {
                Log.d(TAG, "onActivityPaused: " + activity.localClassName)
            }

            override fun onActivityStopped(activity: Activity) {
                Log.d(TAG, "onActivityStopped: " + activity.localClassName)
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
                Log.d(TAG, "onActivitySaveInstanceState: " + activity.localClassName)
            }

            override fun onActivityDestroyed(activity: Activity) {
                Log.d(TAG, "onActivityDestroyed: " + activity.localClassName)
            }

        })
    }
}